package com.cj.stock.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author cheng jun
 * @Description:
 * @Date 2021/3/18 17:35
 */
@Configuration
@EnableAsync
public class ThreadConfig {

    @Value("${thread.number0fCoreThreads}")
    private int number0fCoreThreads;

    @Value("${thread.maximumNumberOfThreads}")
    private int maximumNumberOfThreads;

    @Value("${thread.queueCapacity}")
    private int queueCapacity;

    @Value("${thread.threadActiveTime}")
    private int threadActiveTime;

    @Value("${thread.threadName}")
    private String threadName;

    @Bean("taskExecutor")
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(number0fCoreThreads);
        executor.setMaxPoolSize(maximumNumberOfThreads);
        executor.setQueueCapacity(queueCapacity);
        executor.setKeepAliveSeconds(threadActiveTime);
        executor.setThreadNamePrefix(threadName);
        // 设置拒绝策略（抛弃策略）
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 等待所有任务结束后再关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);

        return executor;
    }

    public static void main(String[] args) {
        /*int n = 200000;
        int count = n / 2000;
        System.out.println(count);
        for (int i = 0; i <= count; i++) {
            System.out.println(i * 2000);
        }*/
        System.out.println(10/100);
    }




























}
